/**
 * Created by Administrator on 2017/6/13.
 */
angular
    .module('myApp', [], ['$compileProvider', function ($compileProvider) {
        $compileProvider.directive('customTags', function () {
            return {
                restrict: 'AE',
                replace: true,
                template: '<div><p class="h3 bg-danger">教我怎么能不难过 ' +
                ' 你劝我灭了心中的火</p><book-add></book-add></div>',
                controller: function ($scope) {
                    this.addName = function (name) {
                        $scope.$apply(function () {
                            $scope.name.push(name);
                            console.log($scope.name)
                        });
                    }
                },
                link: function (scope, ele, attr, controller) {
                    $(ele).children('p.bg-danger').on('click',function () {
                    });
                }
            }
        })

        $compileProvider.directive('bookAdd', function () {
            return {
                restrict: 'ECMA',
                replace: true,
                require: '^customTags',//向上寻找到customTags这个指令，使用其方法
                template: '<div><input type="text" class="form-control"><button class="btn btn-success" type="button">这是一个按钮</button><p>{{name}}</p></div>',
                link: function ($scope, ele, attr, controller) {
                    var inputVal = $(ele).children('input');
                    $(ele).children('button').on('click', function () {
                        if (inputVal.val()) {
                            controller.addName(inputVal.val());
                        }
                    });
                },
                controller: function ($scope) {
                    this.showText = function () {
                        alert($scope.name);
                    }
                }
            }
        })
    }])
    .controller('MyController', function ($scope) {
        $scope.name = ['宋玉清', '朱明宇', '李俊'];
    });